<?php
/**
 * @version $Id: image_response.class.php 5 2009-12-16 11:15:17Z oystein.rg $
 * @licence http://www.opensource.org/licenses/bsd-license.php The BSD License
 * @copyright Upstruct Berlin Oslo
 */

/**
 * @author     Øystein Riiser Gundersen <oystein@upstruct.com>
 * @package    ImageKit
 * @version    $Revision: 5 $
 */
class ImageResponse extends HTTPResponse {

	protected $image;
	
	protected $OUTPUTTERS = array(
		MIMEType::PNG => 'imagepng',
		MIMEType::JPEG => 'imagejpeg',
		MIMEType::GIF => 'imagegif',
	);
	
	protected $outputter = NULL;
	protected $file_path = NULL;
	
	public function __construct($image, $mime_type, $file_path = NULL) {
		parent::__construct();
		$this->image = $image;
		$this->file_path = $file_path;
		$this['Content-Type'] = $mime_type;
		$this->outputter = $this->OUTPUTTERS[$mime_type];
	}
	
	public function flush() {
		$this->sendHeaders();

		ob_start();
			if (!is_null($this->file_path))
				call_user_func($this->outputter, $this->image, $this->file_path);
			call_user_func($this->outputter, $this->image);
		$data = ob_get_clean();
		
		$length = strlen($data);
		$this['Content-Length'] = $length;
		$this->sendHeaders();
		echo $data;
		
	}
}